/** 
 * audit rules disabled 1
 */
package edu.gatech.oad.project.operations.scheduling;



import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import edu.gatech.oad.project.coordinator.Office;
import edu.gatech.oad.project.externalcommunication.IOConverter;
import edu.gatech.oad.project.operations.data.Appointment;
import edu.gatech.oad.project.operations.users.Doctor;
import edu.gatech.oad.project.operations.users.Patient;

/**
 * Allows the user to schedule and access appointments
 * @author Group 17
 * @version 1.0
 */
public class Schedule {

    /**
     * Field calender: calender in the package,
     * stores the appointment based on time and
     * then by doctor
     */
    private Calender calender;
  
    /**
     * Field curDate: the current date
     */
    private int curDate;
  
    /**
     * Filed converter: pointer to the converter
     * that connects to the database
     */
    private final IOConverter converter;
    
    /**
     * Field myLogger: holds the logget for the office file
     */
    private static final Logger MYLOGGER = Logger.getLogger("edu.gatech." +
            "oad.project.operations.scheduling");
  
    /**
     * Default Constructor
     * @param converter The IOConverter shared by all classes
     */
    public Schedule(IOConverter converter) {
        
        MYLOGGER.addHandler(Office.FILEHANDLER);
        MYLOGGER.setLevel(Level.ALL);
        
        final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        final Calendar cal = Calendar.getInstance();
        final String date = dateFormat.format(cal.getTime());
        try{
            this.curDate = (Integer.parseInt(date)); 
        }
        catch (NumberFormatException e){
            this.curDate = -1;
            MYLOGGER.severe("Curent Date found an exception when initializing");
            e.printStackTrace();
        }
        
        this.calender = null;
        this.converter = converter;
        MYLOGGER.finest("appointment created");
        
        
    }
 
    /**
     * @return appointments for the current day
     */
    public List<Appointment> getCalendar() {
        return this.getCalendar(null);
    }
 
    /**
     * @param doc the doctor
     * @return appointments for a given doctor for the current day
     */
    public List<Appointment> getCalendar(Doctor doc) {
        calender = new Calender(this.curDate, doc, this.converter);
        return calender.getApps();
    }
    
    /**
     * @param id the apppointment id
     * @param date date of appointment
     * @param time time of appointment
     * @param doc doctor
     * @param pat the patient
     * @param reason reason for appointment
     */
    public void scheduleApp(int id, int date, int time, Doctor doc,  // $codepro.audit.disable largeNumberOfParameters
                                Patient pat, String reason) {
        final Appointment app = new Appointment(id, date, time, doc, pat, reason);
        if(this.isAvailable(app)) {
            this.add(app);
        }
        else {
            this.reject(app);
        }
    }
 
    /**
     * @param n Appointment to check
     * @return true if available, false if not
     */
    public boolean isAvailable(Appointment n) {
        this.calender = new Calender(n.getDate(), this.converter);
        return true;
        //return this.calender.isAvailable(int time, Doctor doc);
    }

    /**
     * @param app Appointment to schedule
     */
    public void add(Appointment app) {
        final int id = converter.addAppointment(app);
        app.setID(id);
        //Need to add some call to the GUI to say "An appointment has been scheduled on ......"
    }
    
    /**
     * @param app Appointment to reject
     */
    public void reject(Appointment app) {
        app.cancel();
        //Need to add some call to the GUI to say "Sorry, *Doctor's name* is not available for that time" and suggest
         //other doctors for that time
    }
    
    /**
     * Removes the appointment from the database
     * 
     * @param app the appointment to remove
     */
    public void cancelApp(Appointment app) {
        converter.deleteAppointment(app);
        app.cancel();
        //Need to add some call to the GUI to say "Your appointment has been cancelled."
    }

    /**
     * Returns the string representation of a schedule
     * 
     * @return the String representation
     */
    @Override
    public String toString() {
        return "Schedule [curDate=" + curDate + "]";
    }
    
    

}
